package com.cm.projectx.controller.analysis;

import com.cm.projectx.controller.analysis.handler.PersonLifeAndExpenseHandler;
import com.cm.projectx.entity.analysis.ReqDto;
import com.cm.projectx.entity.analysis.RespDto;
import com.cm.projectx.response.Result;
import com.cm.projectx.service.analysis.IEggMilkSaltPriceAnalysisService;
import com.cm.projectx.service.analysis.IOilPriceAnalysisService;
import com.cm.projectx.util.PoiHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("v2api/analysis")
public class DataAnalysisController {
    @Autowired
    private PersonLifeAndExpenseHandler personLifeAndExpenseHandler;

    @Resource
    private IEggMilkSaltPriceAnalysisService eggMilkSaltPriceAnalysisService;

    @Resource
    private IOilPriceAnalysisService oilPriceAnalysisService;


    //todo
    //居民生活消费（食）品零售价格监测周报表（超市）  数据分析
    //1.居民生活消费（食）品零售价格监测周报表（超市）  数据分析  后续四个接口均复用
    @RequestMapping(value = "personLifeAndExpensePriceWithSupermarket", method = {RequestMethod.POST})
    public Result personLifeAndExpensePriceWithSupermarket(@RequestBody ReqDto reqDto) {
        Result result = new Result();
        List<RespDto> respDtoList = new ArrayList<>();
        result.setCode(200);
        result.addData("priceResult", respDtoList);
        //1.根据任务和时间查询流程
        //任务id
        int taskId = reqDto.getTaskId();
        //上期报价时间
        String lastReportDate = StringUtils.isEmpty(reqDto.getLastReportDate()) ? reqDto.getNowReportDate() : reqDto.getLastReportDate();
        //当期报价时间
        String nowReportDate = reqDto.getNowReportDate();
        String productName = reqDto.getProductName();
        personLifeAndExpenseHandler.wrapperResult(result, taskId, lastReportDate, nowReportDate, respDtoList, productName);
        return result;
    }

    //居民生活消费（食）品零售价格监测周报表（超市）  数据分析  导出
    @RequestMapping(value = "personLifeAndExpensePriceWithSupermarketExport", produces = {"application/octet-stream"}, method = {RequestMethod.POST})
    public void personLifeAndExpensePriceWithSupermarketExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.personLifeAndExpensePriceWithSupermarketExportHandler(monitorSet, priceResult, "居民生活消费（食）品零售价格监测周报表（超市）.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }

    //2.一期报表  数据分析
    @RequestMapping(value = "firstTimeReport", method = {RequestMethod.POST})
    public Result firstTimeReport(@RequestBody ReqDto reqDto) {
        Result result = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        result.addData("needMutiTableTitle", true);
        return result;
    }

    //一期报表 数据分析  导出
    @RequestMapping(value = "firstTimeReportExport", produces = {"application/octet-stream"}, method = {RequestMethod.POST})
    public void firstTimeReportExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.firstTimeReportExportHandler(monitorSet, priceResult, "一期报表.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }

    //2.居民生活消费（食）品零售价格监测周报表（集市）  数据分析
    @RequestMapping(value = "personLifeAndExpensePriceWithMart", method = {RequestMethod.POST})
    public Result personLifeAndExpensePriceWithMart(@RequestBody ReqDto reqDto) {
        return this.personLifeAndExpensePriceWithSupermarket(reqDto);
    }

    //居民生活消费（食）品零售价格监测周报表（集市）  数据分析  导出
    @RequestMapping(value = "personLifeAndExpensePriceWithMartExport", produces = {"application/octet-stream"}, method = {RequestMethod.POST})
    public void personLifeAndExpensePriceWithMartExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.personLifeAndExpensePriceWithMartExportHandler(monitorSet, priceResult, "居民生活消费（食）品零售价格监测周报表（集市）.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }

    //3、农产品批发市场粮油价格  数据分析
    @RequestMapping(value = "marketPricesOfGrainsAndOils", method = {RequestMethod.POST})
    public Result marketPricesOfGrainsAndOils(@RequestBody ReqDto reqDto) {
        return this.personLifeAndExpensePriceWithSupermarket(reqDto);
    }

    //农产品批发市场粮油价格  数据分析  导出
    @RequestMapping(value = "marketPricesOfGrainsAndOilsExport", produces = {"application/octet-stream"}, method = {RequestMethod.POST})
    public void marketPricesOfGrainsAndOilsExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.personLifeAndExpensePriceWithMartExportHandler(monitorSet, priceResult, "农产品批发市场粮油价格.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }

    //4、城市居民日用工业消费品零售价格  数据分析
    @RequestMapping(value = "retailPricesOfDailyIndustrialConsumerGoods", method = {RequestMethod.POST})
    public Result retailPricesOfDailyIndustrialConsumerGoods(@RequestBody ReqDto reqDto) {
        return this.personLifeAndExpensePriceWithSupermarket(reqDto);
    }

    //城市居民日用工业消费品零售价格  数据分析  导出
    @RequestMapping(value = "retailPricesOfDailyIndustrialConsumerGoodsExport", produces = {"application/octet-stream"}, method = {RequestMethod.POST})
    public void retailPricesOfDailyIndustrialConsumerGoodsExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.personLifeAndExpensePriceWithMartExportHandler(monitorSet, priceResult, "城市居民日用工业消费品零售价格.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }


    /**
     * 鸡蛋价格监测报表 数据分析
     * @param reqDto 请求参数
     * @return 数据分析结果
     */
    @PostMapping(value = "/egg")
    public Result egg(@RequestBody ReqDto reqDto){

//        return eggMilkSaltPriceAnalysisService.getPriceAnalysis(reqDto);
        return this.personLifeAndExpensePriceWithSupermarket(reqDto);

    }

    /**
     * 鸡蛋价格监测报表 数据分析 导出
     * @param reqDto 请求参数
     * @param response http返回对象
     */
    @PostMapping("/egg/export")
    public void eggExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
//        eggMilkSaltPriceAnalysisService.exportPriceAnalysis(reqDto, response, "egg");
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.personLifeAndExpensePriceWithMartExportHandler(monitorSet, priceResult, "鸡蛋价格监测报表.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }

    /**
     * 牛奶及相关商品价格监测报表 数据分析
     * @param reqDto 请求参数
     * @return 数据分析结果
     */
    @PostMapping("/milk")
    public Result milkAndRelatedProducts(@RequestBody ReqDto reqDto){
//        return eggMilkSaltPriceAnalysisService.getPriceAnalysis(reqDto);
        return this.personLifeAndExpensePriceWithSupermarket(reqDto);
    }

    /**
     * 牛奶及相关商品价格监测报表 数据分析 导出
     * @param reqDto 请求参数
     * @param response http返回对象
     */
    @PostMapping("/milk/export")
    public void milkAndRelatedProductsExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
//        eggMilkSaltPriceAnalysisService.exportPriceAnalysis(reqDto, response, "milk");
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.personLifeAndExpensePriceWithMartExportHandler(monitorSet, priceResult, "牛奶及相关商品价格监测报表.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }

    /**
     * 食盐价格监测报表 数据分析
     * @param reqDto 请求参数
     * @return 数据分析结果
     */
    @PostMapping("/salt")
    public Result salt(@RequestBody ReqDto reqDto){
//        return eggMilkSaltPriceAnalysisService.getPriceAnalysis(reqDto);
        return this.personLifeAndExpensePriceWithSupermarket(reqDto);
    }

    /**
     * 食盐价格监测报表 数据分析 导出
     * @param reqDto 请求参数
     * @param response http返回对象
     */
    @PostMapping("/salt/export")
    public void saltExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
        //eggMilkSaltPriceAnalysisService.exportPriceAnalysis(reqDto, response, "salt");
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.personLifeAndExpensePriceWithMartExportHandler(monitorSet, priceResult, "食盐价格监测报表.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }

    /**
     * 农业生产资料监测报表 数据分析
     * @param reqDto 请求参数
     * @return 数据分析结果
     */
    @PostMapping("/agriProduct")
    public Result agriProduct(@RequestBody ReqDto reqDto){
//        return eggMilkSaltPriceAnalysisService.getPriceAnalysis(reqDto);
        return this.personLifeAndExpensePriceWithSupermarket(reqDto);
    }

    /**
     * 农业生产资料格监测报表 数据分析 导出
     * @param reqDto 请求参数
     * @param response http返回对象
     */
    @PostMapping("/agriProduct/export")
    public void agriProductExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
//        eggMilkSaltPriceAnalysisService.exportPriceAnalysis(reqDto, response, "agriProduct");
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.personLifeAndExpensePriceWithMartExportHandler(monitorSet, priceResult, "农业生产资料格监测报表.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }

    /**
     * 小麦、面粉价格检测报表 数据分析
     * @param reqDto 请求参数
     * @return 数据分析结果
     */
    @PostMapping("/wheat")
    public Result wheat(@RequestBody ReqDto reqDto){
//        return eggMilkSaltPriceAnalysisService.getPriceAnalysis(reqDto);
        return this.personLifeAndExpensePriceWithSupermarket(reqDto);
    }

    /**
     * 小麦、面粉价格检测报表 数据分析 导出
     * @param reqDto 请求参数
     * @param response http返回对象
     */
    @PostMapping("/wheat/export")
    public void wheatExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
//        eggMilkSaltPriceAnalysisService.exportPriceAnalysis(reqDto, response, "wheat");
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.personLifeAndExpensePriceWithMartExportHandler(monitorSet, priceResult, "小麦、面粉价格检测报表.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }



    /**
     * 玉米及相关商品价格监测报表
     * @param reqDto 请求参数
     * @return 数据分析结果
     */
    @PostMapping("/corn")
    public Result corn(@RequestBody ReqDto reqDto){
//        return eggMilkSaltPriceAnalysisService.getPriceAnalysis(reqDto);
        return this.personLifeAndExpensePriceWithSupermarket(reqDto);
    }


    /**
     * 玉米及相关商品价格监测报表 数据分析 导出
     * @param reqDto 请求参数
     * @param response http返回对象
     */
    @PostMapping("/corn/export")
    public void cornExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
//        eggMilkSaltPriceAnalysisService.exportPriceAnalysis(reqDto, response, "corn");
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.personLifeAndExpensePriceWithMartExportHandler(monitorSet, priceResult, "玉米及相关商品价格监测报表.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }



    /**
     * 食用油价格监测报表
     * @param reqDto 请求参数
     * @return 数据分析结果
     */
    @PostMapping("/cookingOilPrice")
    public Result cookingOilPrice(@RequestBody ReqDto reqDto){
//        return eggMilkSaltPriceAnalysisService.getPriceAnalysis(reqDto);
        return this.personLifeAndExpensePriceWithSupermarket(reqDto);
    }

    /**
     * 食用油价格监测报表 数据分析 导出
     * @param reqDto 请求参数
     * @param response http返回对象
     */
    @PostMapping("/cookingOilPrice/export")
    public void cookingOilPriceExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
//        eggMilkSaltPriceAnalysisService.exportPriceAnalysis(reqDto, response, "cookingOilPrice");
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.personLifeAndExpensePriceWithMartExportHandler(monitorSet, priceResult, "食用油价格监测报表.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }



    /**
     * 生猪及相关商品价格监测报表
     * @param reqDto 请求参数
     * @return 数据分析结果
     */
    @PostMapping("/pork")
    public Result porkPrice(@RequestBody ReqDto reqDto){
//        return eggMilkSaltPriceAnalysisService.getPriceAnalysis(reqDto);
        return this.personLifeAndExpensePriceWithSupermarket(reqDto);
    }

    /**
     * 生猪及相关商品价格监测报表 数据分析 导出
     * @param reqDto 请求参数
     * @param response http返回对象
     */
    @PostMapping("/pork/export")
    public void porkExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
//        eggMilkSaltPriceAnalysisService.exportPriceAnalysis(reqDto, response, "pork");
        Result searchResult = this.personLifeAndExpensePriceWithSupermarket(reqDto);
        List<RespDto> priceResult = (List<RespDto>) searchResult.getData().get("priceResult");
        List<String> monitorSet = (List<String>) searchResult.getData().get("monitorSet");
        //处理excele的写入逻辑
        Map<String, Object> fileData = personLifeAndExpenseHandler.personLifeAndExpensePriceWithMartExportHandler(monitorSet, priceResult, "生猪及相关商品价格监测报表.xlsx");
        PoiHelper.downFileByBrowser(fileData, response);
    }

    /**
     * 食用油 数据分析
     * @param reqDto 请求参数
     * @return 数据分析结果
     */
    @PostMapping("/oil")
    public Result oil(@RequestBody ReqDto reqDto){
        return oilPriceAnalysisService.getPriceAnalysis(reqDto);
    }

    /**
     * 食用油数据分析 导出
     * @param reqDto 请求参数
     * @param response http返回对象
     */
    @PostMapping("/oil/export")
    public void oilExport(@RequestBody ReqDto reqDto, HttpServletResponse response) {
        oilPriceAnalysisService.exportPriceAnalysis(reqDto, response, "oil");
    }

}
